package com.ebelter.btcomlib.utils.log;

import android.text.TextUtils;



import org.apache.log4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LogUtils {

    private static boolean isConfigured = false;

    public static final int MODE_INFO = 3;
    public static final int MODE_WARM = 4;
    public static final int MODE_ERROR = 5;
    public static final int MODE_PRODUCT = 6;//不打印所有日志
    /**
     * 决定Log是否打印，默认为MODE_DEBUG模式，供开发人员使用，发版前将根据AppConfig，指定对应的Log级别
     **/
    private static int logMode = MODE_INFO;
    //	开启单线程线程池线程输出Log
    private static ExecutorService logService = Executors.newSingleThreadExecutor();
    public static final boolean isDebug = true;

    //	不允许实例化
    private LogUtils() {}




    public static void i(final String tag, final String msg) {
        if (logMode <= MODE_INFO) {
            final String threadName = Thread.currentThread().getName();
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.info("线程Name = " + threadName + " # "  + msg);
                }
            });
        }

    }

    public static void i2(final String tag, final String msg) {
        if (true) {//这里打包的时候要关闭掉 重要的的数据开发的时候这里打开
            final String threadName = Thread.currentThread().getName();
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.info("线程Name = " + threadName + " # "  + msg);
                }
            });
        }

    }

    public static void w(final String tag, final String msg) {

        if (logMode <= MODE_WARM) {
            final String threadName = Thread.currentThread().getName();
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.warn("线程Name = " + threadName + " # " + msg);
                }
            });
        }
    }

    public static void e(final String tag, final String msg) {
        final String threadName = Thread.currentThread().getName();
        if (logMode <= MODE_ERROR) {
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.error("线程Name = " + threadName + " # " + msg);
                }
            });
        }
    }

    private static Logger getLogger(String tag) {
        if (!isConfigured) {
            Log4jConfigure.configure();
            isConfigured = true;
        }
        Logger logger;
        if (TextUtils.isEmpty(tag)) {
            logger = Logger.getRootLogger();
        } else {
            logger = Logger.getLogger(tag);
        }
        return logger;
    }
}